package cn.iocoder.yudao.module.brokerage.merchant.mq;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.module.api.shop.revenue.message.RevenueCancelMessage;
import cn.iocoder.yudao.module.api.shop.revenue.message.RevenueUnfreezeMessage;
import cn.iocoder.yudao.module.api.trade.order.dto.TradeOrderItemRespDTO;
import cn.iocoder.yudao.module.api.trade.order.dto.TradeOrderRespDTO;
import cn.iocoder.yudao.module.api.trade.order.message.TradeAfterSaleCompletedMessage;
import cn.iocoder.yudao.module.api.trade.order.message.TradeOrderCompletedMessage;
import cn.iocoder.yudao.module.api.trade.order.message.TradeOrderPaidMessage;
import cn.iocoder.yudao.module.api.trade.scan.dto.ScanOrderDTO;
import cn.iocoder.yudao.module.api.trade.scan.message.ScanOrderPaidMessage;
import cn.iocoder.yudao.module.brokerage.merchant.dao.BrokerageMerchantRecordMapper;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.po.AgencyBrokerageRecordDO;
import cn.iocoder.yudao.module.brokerage.merchant.service.AppBrokerageMerchantService;
import cn.iocoder.yudao.module.brokerage.merchant.service.BrokerageMerchantRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 推广商家 事件消费者
 *
 * @author 山野羡民
 */
@Component
@Slf4j
public class BrokerageAgencyConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(TradeOrderPaidMessage message) {
        TradeOrderRespDTO order = message.getOrder();
        log.info("[brokerage/agency][onMessage][交易订单支付事件({})]", order.getNo());
        SpringUtil.getBean(BrokerageMerchantRecordService.class).addBrokerageWhenOrderPaid(order);
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(TradeOrderCompletedMessage message) {
        TradeOrderRespDTO order = message.getOrder();
        log.info("[brokerage/agency][onMessage][交易订单完成事件({})]", order.getNo());
        List<AgencyBrokerageRecordDO> records = SpringUtil.getBean(BrokerageMerchantRecordMapper.class).selectByTradeNo(order.getNo());
        if (CollUtil.isEmpty(records)) {
            log.warn("[brokerage/agency][onMessage][交易订单完成未找到对应的佣金记录：{}", order.getNo());
            return;
        }
        BrokerageMerchantRecordService brokerageMerchantRecordService = SpringUtil.getBean(BrokerageMerchantRecordService.class);
        for (AgencyBrokerageRecordDO record : records) {
            brokerageMerchantRecordService.unfreezeRecord(record);
        }
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(TradeAfterSaleCompletedMessage message) {
        TradeOrderRespDTO order = message.getOrder();
        log.info("[brokerage/agency][onMessage][交易订单售后完成事件({})]", order.getNo());
        TradeOrderItemRespDTO orderItem = message.getOrderItem();
        Assertions.notNull(orderItem, "交易订单售后完成事件携带的订单项信息为空");
        SpringUtil.getBean(BrokerageMerchantRecordService.class).cancelBrokerage(order, orderItem);
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ScanOrderPaidMessage message) {
        ScanOrderDTO order = message.getOrder();
        log.info("[brokerage/agency][onMessage][线下扫码订单支付完成事件({})]", order.getTradeNo());
        SpringUtil.getBean(BrokerageMerchantRecordService.class).addBrokerageWhenOrderPaid(order);
        List<AgencyBrokerageRecordDO> records = SpringUtil.getBean(BrokerageMerchantRecordMapper.class).selectByTradeNo(order.getTradeNo());
        if (CollUtil.isEmpty(records)) {
            log.warn("[brokerage/agency][onMessage][线下扫码订单完成未找到对应的佣金记录：{}", order.getTradeNo());
            return;
        }
        BrokerageMerchantRecordService brokerageMerchantRecordService = SpringUtil.getBean(BrokerageMerchantRecordService.class);
        for (AgencyBrokerageRecordDO record : records) {
            brokerageMerchantRecordService.unfreezeRecord(record);
        }
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(RevenueUnfreezeMessage message) {
        log.info("[pay/splitting][onMessage][商家营收结算事件({})]", message);
        SpringUtil.getBean(AppBrokerageMerchantService.class).increaseSettlementMoney(message.getShopId(), message.getPayPrice(), message.getSettlementMoney());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(RevenueCancelMessage message) {
        log.info("[pay/splitting][onMessage][商家营收取消事件({})]", message);
        SpringUtil.getBean(AppBrokerageMerchantService.class).decreaseSettlementMoney(message.getShopId(), message.getPayPrice(), message.getSettlementMoney());
    }

}
